import { shallowRef } from "vue";
import * as ElIcons from "@element-plus/icons-vue";
//全局通用方法
//格式化日期
const formatDate = (datestr: string, format: string = 'yyyy-MM-dd HH:mm:ss') => {
    if(!!!datestr){
        return null;
    }
    const date = new Date(datestr);
    const pad = (n: number) => n.toString().padStart(2, '0');
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, (token) => {
        switch (token) {
            case 'yyyy':
                return date.getFullYear().toString();
            case 'MM':
                return pad(date.getMonth() + 1);
            case 'dd':
                return pad(date.getDate());
            case 'HH':
                return pad(date.getHours());
            case 'mm':
                return pad(date.getMinutes());
            case 'ss':
                return pad(date.getSeconds());
            default:
                return token;
        }
    });
};


//icon图片解析
const getIconData = () => {
    const data: { [key: string]: any } = {};
    Object.entries(ElIcons).forEach(([key, value]) => {
        data[key] = value;
    });
    return data;
};
const iconMap = shallowRef({
    iconData: getIconData(),
});

//检查是否有权限
const chkgrant=(key,method)=>{
    let grant=JSON.parse(localStorage.getItem('grant'));
    if(!grant){
        return false;
    }
    if(grant['isadmin']==0){
        if(!grant['grantlist'] || grant['grantlist'].lenght==0){
            return false;
        }
        let isgrant=false;
        for(let i=0;i<grant['grantlist'].length;i++){
            if(grant['grantlist'][i]['name']==key){
                if(grant['grantlist'][i][method] && grant['grantlist'][i][method].select==true){
                    isgrant=true;
                    break;
                }
            }
        }
        if(!isgrant){
            return false;
        }
    }
    return true;
}
export {
    iconMap,
    formatDate,
    chkgrant
}